//========================================================================
// FILENAME : adc.h
// CREATED : 23-02-2009
// AUTHOR : Jesper Aagaard Vuholm
// DESCR. : This file contains data used for our AD-conversions.
//
//------------------------------------------------------------------------
//
// REV. DATE/AUTHOR CHANGE DESCRIPTION
// 1.00 24-02-2009/Jesper basic prototype declarations added.
// 1.01 27-02-2009/Jesper prioritized channel selector for the ADMUX added.
//                        see the notes for: priority_list[PRIORITY_LIST_SIZE]
// 1.03 15-03-2009/Jesper 3 ADC channels removed as we wont need them.
//========================================================================
#include <avr/io.h>

// ADC setup
#define ADC_CHANNELS 3
#define PRIORITY_LIST_SIZE 255
#define SAMPLES 10

// Index defines
#define ACC 0
#define GYRO 1
#define JOYSTICK 2

unsigned int tmpVal;
unsigned char priority_index; //Auto-incrementation will make it flip, saves us time in the ISR.
unsigned int ADC_raw_values[ADC_CHANNELS];

//=============================================================
// DESCR. : Hard-coded priority list, gives a simple and very
// fast decision making. Used for setting up the ADMUX register.
//
// Usage. : Setup the table below until you have the "visit freq."
// for every pin as you want. Once setup, all you need to do is
// to increment the pointer(for this table), this will decide what
// channel the next ADC-conversion will come from.
//
// Size. : As for now the list will have 255 entries, that mean
// 1 entry will equal a AD conversion from that channel 0.392%
// of the time. If we want a higher resolution, we can just
// change the list size, with respect to our indexer to fit our needs.
//
// Channel to physical setup:
// *Channel*           *Physical*
//  0                   Accelerometer.
//  1                   Gyroscope.
//  2                   Joystick.
//=============================================================
static const unsigned char priority_list[PRIORITY_LIST_SIZE] =
{               //0-99
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,2, //Joystick
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                //100-199
                0,1,0,1,0,1,0,1,0,2, //Joystick
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,2, //Joystick
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                //200-254
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,2, //Joystick
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0,1,0,1,0,1,
                0,1,0,1,0
};

// public functions
void ADC_init(void);
void ADC_stop(void);
unsigned int getJoystick(void);
unsigned int getAcclerometer(void);
unsigned int getGyro(void);

// static functions
static void ADC_setPin (unsigned char pin);
static void ADC_start(void);
static void ADC_startScan(void);

